package com.sywb.chuangyebao.contract;

import android.app.Activity;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.ManuscriptManagenetInfo;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.view.ManuscriptStypeActivity;
import org.bining.footstone.adapter.BaseRecyclerAdapter;
import org.bining.footstone.adapter.BaseRecyclerDivider;
import org.bining.footstone.adapter.ViewHolderHelper;
import org.bining.footstone.adapter.listener.OnItemClickListener;
import org.bining.footstone.mvp.IView;
import org.bining.footstone.presenter.BasePresenter;
import org.bining.footstone.rxjava.rxbus.RxBus;
import org.bining.footstone.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

public interface ManuscriptManagementContract {

    interface ManuscriptManagementView extends IView{
        /**
         * 获取 RecyclerView
         */
        RecyclerView getRecyclerView();
    }

    class Presenter extends BasePresenter<ManuscriptManagementView>{
        private ManuscriptManagenetInfo manuscriptManagenetInfo;
        private List<ManuscriptManagenetInfo> list;
        private ManuscriptAdapter adapter;
        protected RecyclerView manuscriptRecyclerView;

        protected BaseRecyclerDivider recyclerDivider;


        private void  initPage(){
            adapter = new ManuscriptAdapter(mActivity);
            adapter.addDatas(initData());
            manuscriptRecyclerView = mView.getRecyclerView();
            manuscriptRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
            manuscriptRecyclerView.setAdapter(adapter);
            adapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(View view, int i) {
                mView.advance(ManuscriptStypeActivity.class,adapter.getItem(i).getTitle(),i);
                }
            });
            queryBalance();
        }

        /**
         * 添加数据源
         * @return
         */
        private List<ManuscriptManagenetInfo> initData(){
            list = new ArrayList<>();
            manuscriptManagenetInfo = new ManuscriptManagenetInfo(R.drawable.manu_article,"我的文章","发表你的长文章");
            list.add(manuscriptManagenetInfo);
            manuscriptManagenetInfo = new ManuscriptManagenetInfo(R.drawable.manu_video,"我的视频","发布你的视频");
            list.add(manuscriptManagenetInfo);
            return list;
        }

        /**
         * 账户余额
         */
        public void queryBalance(){
            HttpUtils.queryBalance(new HttpCallback<Integer>() {
                @Override
                public void onSuccess(Integer balanceBean) {
                    RxBus.get().post(Constants.POST_QUERY_BALANCE,balanceBean);
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                }

                @Override
                public void onFinish() {
                    super.onFinish();

                }
            });
        }

        @Override
        public void onStart() {
            initPage();
        }
    }
    class ManuscriptAdapter extends BaseRecyclerAdapter<ManuscriptManagenetInfo>{
        public ManuscriptAdapter(Activity activity){
            super(activity, R.layout.item_manuscript_management);
        }
        @Override
        protected void setItemData(ViewHolderHelper viewHolderHelper, final int i, ManuscriptManagenetInfo manuscriptManagenetInfo) {
            viewHolderHelper.setBackgroundRes(R.id.item_manuscript_iv,manuscriptManagenetInfo.getId());
            viewHolderHelper.setText(R.id.item_manuscript_title_tv,manuscriptManagenetInfo.getTitle());
            viewHolderHelper.setText(R.id.item_manuscript_introduce_tv,manuscriptManagenetInfo.getIntroduce());
        }
    }
}
